package com.fescoservice.service;

import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fescoservice.dao.LinkDAO;
import com.fescoservice.exception.FSException;
import com.fescoservice.model.Link;

@Service
public class LinkService {
	private static final Logger log = LogManager.getLogger(LinkService.class);
	@Autowired
	private LinkDAO linkDAO;
	
	public void addLink(Link link){	
	
			linkDAO.save(link);
	}	
	public void deleteLink(int id){		
		Link link = linkDAO.findById(id);
		linkDAO.delete(link);
	}	
	public Link getLink(int id){		
		Link link = linkDAO.findById(id);
		return link;
	}	
	public void updateLink(Link link){	
		
		linkDAO.merge(link);
	}	
	public Integer countLinks(String language,String dept)  throws FSException{
		String sql = "SELECT COUNT(*) AS CNT FROM T_Link C WHERE C.LINK_DEPT= :dept ";
		if(null!=language){
		   sql+="AND C.LINK_LANGUAGE = :language";
	   }
		return linkDAO.countLinks(sql,language,dept);
	}
	public List listLinks(String language,String dept,int first)  throws FSException{
		String hql = "from Link o where o.linkDept= :dept ";
		if(null!=language){
		   hql+="and o.linkLanguage = :language";
		}
		return linkDAO.listLinks(hql,language,dept,first);
	}
}
